<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
      </div>
      <div style="margin-top: 15px" class="flex">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="用户编号：">
            <el-input style="width: 203px" v-model="listQuery.id" placeholder="用户编号"></el-input>
          </el-form-item>
          <el-form-item label="用户名称：">
            <el-input style="width: 203px" v-model="listQuery.nickname" placeholder="用户名称"></el-input>
          </el-form-item>
          <el-form-item label="用户手机：">
            <el-input style="width: 203px" v-model="listQuery.phone" placeholder="用户手机"></el-input>
          </el-form-item>
          <el-form-item label="用户状态：">
            <el-select v-model="listQuery.status" placeholder="全部" clearable>
              <el-option
                v-for="item in formatStatus"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div class="flex">
        <el-button
          @click="handleSearchList()"
          type="primary"
          size="small">
          查询结果
        </el-button>
        <el-button
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
      </div>
    </el-card>

    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <el-table ref="memberTable"
                :data="list"
                style="width: 100%;"
                @selection-change=""
                v-loading="listLoading" border>
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="120" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="用户名称" align="center">
          <template slot-scope="scope">{{scope.row.nickname}}</template>
        </el-table-column>
        <el-table-column label="用户账号" align="center">
          <template slot-scope="scope">{{scope.row.username}}</template>
        </el-table-column>
        <el-table-column label="手机号" align="center">
          <template slot-scope="scope">{{scope.row.phone }}</template>
        </el-table-column>
        <el-table-column label="状态" width="180" align="center">
          <template slot-scope="scope">
            <p>{{scope.row.status | formatStatus}}：
              <el-switch
                @change="handleStatusChange(scope.$index, scope.row)"
                :active-value="1"
                :inactive-value="0"
                v-model="scope.row.status">
              </el-switch>
            </p>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>

  </div>
</template>

<script>
    import {fetchList, memberStatus as memberStatusUpdate} from '@/api/member';

    const defaultListQuery = {
        pageNum: 1,
        pageSize: 10,
        id: null,
        nickname: null,
        phone: null,
        status:null
    };
    export default {
        name: "memberList",
        data() {
            return {
                listQuery: Object.assign({}, defaultListQuery),
                listLoading: true,
                list: null,
                total: null,
                logisticsDialogVisible: false,
                memberStatus: null,
                formatStatus: [{
                    value: 1,
                    label: '启用中'
                }, {
                    value: 0,
                    label: '已禁用'
                }]
            }
        },
        created() {
            this.getList();
        },
        filters: {
            formatStatus(status) {
                if (status === 1) {
                    return '启用中';
                } else {
                    return '已禁用';
                }
            }
        },
        methods: {
            handleResetSearch() {
                this.listQuery = Object.assign({}, defaultListQuery);
            },
            handleSearchList() {
                this.listQuery.pageNum = 1;
                this.getList();
            },
            handleSizeChange(val) {
                this.listQuery.pageNum = 1;
                this.listQuery.pageSize = val;
                this.getList();
            },
            handleCurrentChange(val) {
                this.listQuery.pageNum = val;
                this.getList();
            },
            handleDelete(index, row) {
                this.deleteMember(row.id);
            },
            handleShowData(index, row) {
                console.log("handleShowProduct", index);
                console.log("handleShowProduct", row);
                this.memberShowData(row);
            },
            memberShowData(data) {
                this.$router.push({path: '/ums/memberShow', query: data});
            },
            deleteMember(ids) {
                this.$confirm('是否要删除该用户?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                })
            },
            getList() {
                this.listLoading = true;
                fetchList(this.listQuery).then(response => {
                    this.listLoading = false;
                    this.list = response.data.list;
                    this.total = response.data.total;
                })
            },
            handleStatusChange(index, row) {
                this.memberStatus = row.status;
                console.info("row.status" + row.status);
                this.$confirm('是否要改变该用户状态?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    debugger
                    let data = {
                        id: row.id,
                        status: this.memberStatus
                    }
                    memberStatusUpdate(data);
                })
            }
        }
    }
</script>

<style>

</style>
